package zelix.utils.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class GET extends JFrame {

	private JPanel contentPane;
	private JTextField qqname;
	private String QQ ;
	private String bt = "\u67E5Q\u7ED1-<�Զ���������>";
	private String OKQQ ;
	private String Sjgsd ;
	private JLabel lblNewLabel_1;
	private JLabel qqokname;


	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { Main frame = new Main();
	 * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } });
	 * }
	 */

	/**
	 * Create the frame.
	 */
	public GET() {
		setResizable(false);
		setTitle(bt);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 495, 305);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u8F93\u5165\u4F60\u8981\u67E5\u8BE2\u7684QQ\u53F7\uFF1A");
		lblNewLabel.setFont(new Font("΢���ź�", Font.BOLD, 18));
		lblNewLabel.setBounds(10, 26, 236, 54);
		contentPane.add(lblNewLabel);
		
		qqname = new JTextField();
		qqname.setHorizontalAlignment(SwingConstants.LEFT);
		qqname.setFont(new Font("����", Font.BOLD, 20));
		qqname.setToolTipText("\u8BF7\u8F93\u5165\u6570\u5B57");
		qqname.setBounds(48, 81, 378, 38);
		contentPane.add(qqname);
		qqname.setColumns(10);
		lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setFont(new Font("����", Font.BOLD, 17));
		lblNewLabel_1.setBounds(116, 129, 236, 38);
		contentPane.add(lblNewLabel_1);
		JButton btnNewButton = new JButton("\u5F00\u59CB\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(isInteger(qqname.getText())) {
					setTitle(bt+" ����Ŭ���Ĳ�ѯ�У����Ժ󡤡���������");
					QQ = qqname.getText();
					try {
						OKQQ = get("http://api.cjsrcw.cn/qb-api.php?mod=cha&qq="+QQ);
							if(OKQQ != null) {
								String OK = OKQQ.substring(OKQQ.indexOf("\"mobile\"")+11,OKQQ.indexOf("\"mobile\"")+22);
								if(isInteger(OK)) {
									setTitle(bt+" ��ѯ�ɹ�,��Ϊ���Զ����ƣ�");
									lblNewLabel_1.setText("��ѯ�ɹ�,��Ϊ���Զ����ƣ�");
										//Sjgsd = get("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+OK);
									//	String SJOK = Sjgsd.substring(Sjgsd.indexOf("carrier")+9,Sjgsd.indexOf("carrier")+13);
										
										String gui = get("https://www.sogou.com/websearch/phoneAddress.jsp?phoneNumber="+OK+"&cb=djsonp1606391530084");
										String guishu = gui.substring(gui.indexOf("(")+2, gui.indexOf(")")-1); 
										////////////System.out.println();
										qqokname.setText("�ֻ��ţ�"+OK+" �ֻ������أ�"+guishu);
										copy("QQ�ţ�"+QQ+" �ֻ��ţ�"+OK+" �ֻ������أ�"+guishu);
					}else {
						lblNewLabel_1.setText("��ѯʧ��,���в�û�������¼");
						setTitle(bt+" ��ѯʧ��,���в�û�������¼");
						}
			}else {
						lblNewLabel_1.setText("��ѯ�ӿ���ʧЧ,�����Ա���£�");
				}
			} catch (IOException e1) {
						//////////System.out.println("�����쳣");
						e1.printStackTrace();
		}
				
				}else {
					lblNewLabel_1.setText("\u8BF7\u8F93\u5165\u6B63\u786E\u7684QQ\u53F7\u7801\uFF01");
				}
			}
		});
		btnNewButton.setFont(new Font("΢���ź� Light", Font.BOLD, 18));
		btnNewButton.setToolTipText("\u5F00\u59CB\u67E5\u8BE2");
		btnNewButton.setBounds(90, 206, 296, 45);
		contentPane.add(btnNewButton);
		
		qqokname = new JLabel("");
		qqokname.setFont(new Font("����", Font.BOLD, 17));
		qqokname.setBounds(10, 177, 469, 25);
		contentPane.add(qqokname);
		
		
	}
	public void copy(String str)
    {
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(str), (ClipboardOwner)null);
    }
	public static boolean isInteger(String str) { 
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); 
        return pattern.matcher(str).matches(); 
}
	private static String get(String url) throws IOException
    {
        HttpURLConnection con = (HttpURLConnection)(new URL(url)).openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null)
        {
            response.append(inputLine);
            response.append("\n");
        }

        in.close();
        return response.toString();
    }
}
